<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<script type="text/javascript" src="./js/jquery-1.10.2.js"></script>
<script type="text/javascript">
var save_gubun=0;

function selectGwamok(gwamok){
	var gwamok_code=gwamok.options[gwamok.selectedIndex].value;

	$.ajax({
		type:"post",
		data:{gwamok_code : gwamok_code},
		url:"selectGwamok.do",
		dataType:"json",
		success:function(result){
		 
		 /* alert("과목코드 : "+result.code+"\n"+
				 "과목명 : " +result.name+"\n"+
				 "학점 : " +result.hakjum+"\n"+
				 "전공구분 : " +result.junkong_gubun+"\n"+
				 "강의시간 : "+result.sigan);
		  */
		 //선택한 과목에 대해 받은 값 화면에 세팅
		 $('#gwamok_code').val(result.code);
		 $('#gwamok_name').val(result.name);
		 
		 $('#hakjum').val(result.hakjum);
		 $('#hakjum1').html(result.hakjum);
		 
		 $('#junkong_gubun').val(result.junkong_gubun);
		 $('#junkong_gubun1').html(result.junkong_gubun);
		 
		 $('#sigan').val(result.sigan);
		 $('#sigan1').html(result.sigan);
		 $('#gwa_code').val(result.gwa_code);
		 
		},
		error:function(e){
			alert('실패');
			alert('data :' + result);
		}
			
	});
	
}

function selectGyosu(gyosulist){
	var gyosu_code=gyosulist.options[gyosulist.selectedIndex].value;
		
	$.ajax({
		type:"post",
		data:{gyosu_code : gyosu_code},
		url:"selectGyosu.do",
		dataType:"json",
		success:function(result){
			/*  alert("교수사번 : "+result.gyosu_code+"\n"+
					 "교수이름 : "+result.gyosu_name);  */
			 $('#gyosu_code').val(result.gyosu_code);
			 $('#gyosu_name').val(result.gyosu_name);
			 
		},
		error:function(e){
			alert('실패');
			alert('data :' + result);
		}
			
	});
	
}

function selectRoom(roomlist){
	var room_code=roomlist.options[roomlist.selectedIndex].value;

	$('#room_code').val(room_code);
	
	$.ajax({
		type:"post",
		data:{room_code : room_code},
		url:"selectRoom.do",
		dataType:"json",
		success:function(result){
			 /* alert("강의실번호 : "+result.room_code+"\n"+
					 "강의실이름 : "+result.room_name); */ 
			 $('#room_code').val(result.room_code);
			 $('#room_name').val(result.room_name);
			 
		},
		error:function(e){
			alert('실패');
			alert('data :' + result);
		}
			
	});
}


function insertSugang(){
	
	var admin_seq = $('#admin_seq').val();
	var gwamok_code = $('#gwamok_code').val();
	var gwamok_name = $('#gwamok_name').val();
	var hakjum = $('#hakjum').val();
	var gyosu_code = $('#gyosu_code').val();
	var gyosu_name = $('#gyosu_name').val();
	var room_code = $('#room_code').val();
	var room_name = $('#room_name').val();
	var sigan = $('#sigan').val();
	var junkong_gubun = $('#junkong_gubun').val();
	var full_inwon = $('#full_inwon').val();
	var curr_inwon = $('#curr_inwon').val();
	var full_yn = $('#full_yn').val();
	var close_yn = $('input:radio[name=close_yn]:checked').val();
	var gwa_code= $('#gwa_code').val();
	
	var data={"gwamok_code":gwamok_code, "gwamok_name":gwamok_name,
					"hakjum":hakjum,"gyosu_code":gyosu_code, "gyosu_name":gyosu_name,
					"room_code":room_code, "room_name":room_name, "sigan":sigan,
					"junkong_gubun":junkong_gubun, "full_inwon":full_inwon,
					"curr_inwon":curr_inwon, "full_yn":full_yn, "close_yn":close_yn,
					"gwa_code":gwa_code};
	
	
	/* alert("저장부분");
	alert("과목코드 : " + gwamok_code + "\n"+
			"과목명 : " + gwamok_name + "\n"+
			"학점 : " + hakjum + "\n"+
			"교수코드 : " + gyosu_code + "\n"+
			"교수이름 : " + gyosu_name + "\n"+
			"강의실번호 : " + room_code + "\n"+
			"강의실이름 : " + room_name + "\n"+
			"강의시간 : " + sigan + "\n"+
			"전공구분 : " + junkong_gubun + "\n"+
			"가능인원 : " + full_inwon + "\n"+
			"신청인원 : " + curr_inwon + "\n"+
			"만원Y/N : " + full_yn + "\n" + 
			"폐강여부 : " + close_yn + "\n"+
			"학과코드 : " + gwa_code ); */
	if(save_gubun!='1'){
		$.ajax({
			type:"post",
			data:data,
			url:"saveSugangAdmin.do",
			dataType:"json",
			success:function(data){
				$("#output").empty();
				
				var head = "<tr id='thfix'><th></th>"+
				"<th>과목코드(과목명)</th><th>교수코드(교수명)</th>"+
				"<th>학점</th><th>전공구분</th><th>강의실코드(강의실)</th>"+
				"<th>강의시간</th><th>신청인원</th>"+
				"<th>가능인원</th><th>만원Y/N</th><th>폐강여부</th>"+
				"<th></th></tr>";
			
			$("#output").append(head);
			
			$(data).each(function(index,item){
				output+="<tr>";
				output+="<td align='center'><input type='checkbox' name='admin_seq' value="+ item.admin_seq +"></td>";
				output+="<td align='center'>" + item.gwamok_code + "("+ item.gwamok_name+")" + "</td>";
				output+="<td align='center'>" + item.gyosu_code +"("+ item.gyosu_name +")" +"</td>";
				output+="<td align='center'>" + item.hakjum + "</td>";
				output+="<td align='center'>" + item.junkong_gubun + "</td>";
				output+="<td align='center'>" + item.room_code + "("+ item.room_name +")"+ "</td>";
				output+="<td align='center'>" + item.sigan + "</td>";
				output+="<td align='center'>" + item.curr_inwon + "</td>";
				output+="<td align='center'>" + item.full_inwon + "</td>";
				output+="<td align='center'>" + item.full_yn + "</td>";
				output+="<td align='center'>" + item.close_yn + "</td>";
				output+='<td align="center"><input type="button" value="수정" onclick="updateSugang(\''+item.admin_seq+'\',\''+item.gwamok_code+'\',\''+item.gwamok_name+'\',\''+item.gyosu_code+'\',\''+item.gyosu_name+'\',\''+item.hakjum+'\',\''+item.junkong_gubun+'\',\''+item.room_code+'\',\''+item.room_name+'\',\''+item.sigan+'\',\''+item.curr_inwon+'\',\''+item.full_inwon+'\',\''+item.full_yn+'\',\''+item.close_yn+'\')"></td>';
				output+="</tr>";
				
				$("#output").append(output);
				output = '';
			});
				
				//초기화
				$('select').find('option:first').attr('selected', 'selected');
				$('#hakjum1').html('학점');
				$('#junkong_gubun1').html('전공구분');
				$('#sigan1').html('강의시간');
				$('#full_inwon').val('');
				$('input:radio[name=close_yn]:input[value=N]').prop('checked',true);
				$('#full_yn1').html('N');
				$('#curr_inwon1').html('0');
				$('#curr_inwon').val('0');
			},
			error:function(e){
				alert('실패');
			}
			
		});
	}else{
		//수정
		var data={"gwamok_code":gwamok_code, "gwamok_name":gwamok_name,
					"hakjum":hakjum,"gyosu_code":gyosu_code, "gyosu_name":gyosu_name,
					"room_code":room_code, "room_name":room_name, "sigan":sigan,
					"junkong_gubun":junkong_gubun, "full_inwon":full_inwon,
					"curr_inwon":curr_inwon, "full_yn":full_yn, "close_yn":close_yn,
					"gwa_code":gwa_code,"admin_seq":admin_seq};
		
		$.ajax({
			type:"post",
			data:data,
			url:"updateSugangAdmin.do",
			dataType:"json",
			success:function(data){
				
				$("#output").empty();
				
				var head = "<tr id='thfix'><th></th>"+
				"<th>과목코드(과목명)</th><th>교수코드(교수명)</th>"+
				"<th>학점</th><th>전공구분</th><th>강의실코드(강의실)</th>"+
				"<th>강의시간</th><th>신청인원</th>"+
				"<th>가능인원</th><th>만원Y/N</th><th>폐강여부</th>"+
				"<th></th></tr>";
			
			$("#output").append(head);
			
			$(data).each(function(index,item){
				output+="<tr>";
				output+="<td align='center'><input type='checkbox' name='admin_seq' value="+ item.admin_seq +"></td>";
				output+="<td align='center'>" + item.gwamok_code + "("+ item.gwamok_name+")" + "</td>";
				output+="<td align='center'>" + item.gyosu_code +"("+ item.gyosu_name +")" +"</td>";
				output+="<td align='center'>" + item.hakjum + "</td>";
				output+="<td align='center'>" + item.junkong_gubun + "</td>";
				output+="<td align='center'>" + item.room_code + "("+ item.room_name +")"+ "</td>";
				output+="<td align='center'>" + item.sigan + "</td>";
				output+="<td align='center'>" + item.curr_inwon + "</td>";
				output+="<td align='center'>" + item.full_inwon + "</td>";
				output+="<td align='center'>" + item.full_yn + "</td>";
				output+="<td align='center'>" + item.close_yn + "</td>";
				output+='<td align="center"><input type="button" value="수정" onclick="updateSugang(\''+item.admin_seq+'\',\''+item.gwamok_code+'\',\''+item.gwamok_name+'\',\''+item.gyosu_code+'\',\''+item.gyosu_name+'\',\''+item.hakjum+'\',\''+item.junkong_gubun+'\',\''+item.room_code+'\',\''+item.room_name+'\',\''+item.sigan+'\',\''+item.curr_inwon+'\',\''+item.full_inwon+'\',\''+item.full_yn+'\',\''+item.close_yn+'\')"></td>';
				output+="</tr>";
				
				$("#output").append(output);
				output = '';
			});
				
				//초기화
				$('select').find('option:first').attr('selected', 'selected');
				$('#hakjum1').html('학점');
				$('#junkong_gubun1').html('전공구분');
				$('#sigan1').html('강의시간');
				$('#full_inwon').val('');
				$('input:radio[name=close_yn]:input[value=N]').prop('checked',true);
				$('#full_yn1').html('N');
				$('#curr_inwon1').html('0');
				$('#curr_inwon').val('0');
				save_gubun = 0;
				
			},
			error:function(e){
				alert('실패');
			}
			
		});
	}

}



function deleteSugang(){
	var arr="";
	
	var admin_seq = $('input[name=admin_seq]');
	
	for(var i = 0; i < admin_seq.length; i++)
	{
		if(admin_seq[i].checked)
		{
			if(i>0)
			{
				arr +=",";
			}
			arr += admin_seq[i].value; 
		}
	}
	
	var data={"checkedAdminseq":arr,"gwa_code":$('#gwa_code').val()};
	
	$.ajax({
		type:"post",
		data:data,
		url:"deleteSugang.do",
		dataType:"json",
		success:function(data){
			$("#output").empty();
			
			var head = "<tr id='thfix'><th></th>"+
			"<th>과목코드(과목명)</th><th>교수코드(교수명)</th>"+
			"<th>학점</th><th>전공구분</th><th>강의실코드(강의실)</th>"+
			"<th>강의시간</th><th>신청인원</th>"+
			"<th>가능인원</th><th>만원Y/N</th><th>폐강여부</th>"+
			"<th></th></tr>";
			
		$("#output").append(head);
		
		$(data).each(function(index,item){
			output+="<tr>";
			output+="<td align='center'><input type='checkbox' name='admin_seq' value="+ item.admin_seq +"></td>";
			output+="<td align='center'>" + item.gwamok_code + "("+ item.gwamok_name+")" + "</td>";
			output+="<td align='center'>" + item.gyosu_code +"("+ item.gyosu_name +")" +"</td>";
			output+="<td align='center'>" + item.hakjum + "</td>";
			output+="<td align='center'>" + item.junkong_gubun + "</td>";
			output+="<td align='center'>" + item.room_code + "("+ item.room_name +")"+ "</td>";
			output+="<td align='center'>" + item.sigan + "</td>";
			output+="<td align='center'>" + item.curr_inwon + "</td>";
			output+="<td align='center'>" + item.full_inwon + "</td>";
			output+="<td align='center'>" + item.full_yn + "</td>";
			output+="<td align='center'>" + item.close_yn + "</td>";
			output+='<td align="center"><input type="button" value="수정" onclick="updateSugang(\''+item.admin_seq+'\',\''+item.gwamok_code+'\',\''+item.gwamok_name+'\',\''+item.gyosu_code+'\',\''+item.gyosu_name+'\',\''+item.hakjum+'\',\''+item.junkong_gubun+'\',\''+item.room_code+'\',\''+item.room_name+'\',\''+item.sigan+'\',\''+item.curr_inwon+'\',\''+item.full_inwon+'\',\''+item.full_yn+'\',\''+item.close_yn+'\')"></td>';
			output+="</tr>";
			
			$("#output").append(output);
			output = '';
		});
		},
		error:function(e){
			
		}
	});
}

function updateSugang(obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9,obj10,obj11,obj12,obj13,obj14){
	
	if(obj14=='N'){
		 $('input:radio[name=close_yn]:input[value=N]').prop('checked',true);
		
	}else{
		 $('input:radio[name=close_yn]:input[value=Y]').prop('checked',true);
	}
	
	$('#admin_seq').val(obj1);
	$('#gwamok_code').val(obj2);
	$("#gwamok").val(obj2);
	$('#gwamok_name').val(obj3);
	$('#gyosu_code').val(obj4);
	$("#gyosulist").val(obj4);
	$('#gyosu_name').val(obj5);
	$('#hakjum').val(obj6);
	$('#hakjum1').html(obj6);
	$('#junkong_gubun').val(obj7);
	$('#junkong_gubun1').html(obj7);
	$('#room_code').val(obj8);
	$("#roomlist").val(obj8);
	$('#room_name').val(obj9);
	$('#sigan').val(obj10);
	$('#sigan1').html(obj10);
	$('#curr_inwon').val(obj11);
	$('#curr_inwon1').html(obj11);
	$('#full_inwon').val(obj12);
	$('#full_yn').val(obj13); 
	$('#full_yn1').html(obj13);
	
	save_gubun = 1;
	
}


	
</script>
</head>
<body>
<br>
<div id="sugangAdminTable1">
	<table >
	<colgroup>
	<col width="240px">
	<col width="160px">
	<col width="70px">
	<col width="80px">
	<col width="160px">
	<col width="130px">
	<col width="70px">
	<col width="100px">
	<col width="100px">
	<col width="150px">
	<col width="70px">
	</colgroup>
		<tr>
			<th>과목코드(과목명)</th>
			<th>교수코드(교수명)</th>
			<th>학점</th>
			<th>전공구분</th>
			<th>강의실코드(강의실)</th>
			<th>강의시간</th>
			<th>신청인원</th>
			<th>가능인원</th>
			<th>만원Y/N</th>
			<th>폐강여부</th>
			<th></th>
		</tr>
		<tr>
			<td align="center">
			<input type="hidden" id="admin_seq">
			<input type="hidden" id="gwa_code" value="${gwa_code}">
			<input type="hidden" id="gwamok_code">
			<input type="hidden" id="gwamok_name">
			
				<select id="gwamok" name="gwamok" onChange="selectGwamok(this)">
					<option selected="selected">${hakgwa.name}</option>
				<c:forEach var="junkonglist" items="${junkonglist}">
					<option value="${junkonglist.code}">${junkonglist.code}(${junkonglist.name})</option>
				</c:forEach>
				</select> 
			</td>
			
			
			<td align="center">
			
			<input type="hidden" id="gyosu_code">
			<input type="hidden" id="gyosu_name">
				<select id="gyosulist" name="gyosulist" onChange="selectGyosu(this)">
					<option selected="selected">${hakgwa.name}</option>
				<c:forEach var="gyosulist" items="${gyosulist}">
					<option value="${gyosulist.sabun}">${gyosulist.sabun}(${gyosulist.name})</option>
				</c:forEach>
				</select>
			</td>
			
			
			<td align="center">
			
			<input type="hidden" id="hakjum" name="hakjum" value="">
			
			<span id="hakjum1">학점</span>
			</td>
			
			<td align="center">
			
			<input type="hidden" id="junkong_gubun" value="">
			
			<span id="junkong_gubun1">전공구분</span>
			</td>
			
			<td align="center">
			
			<input type="hidden" id="room_code">	
			<input type="hidden" id="room_name">	
				<select id="roomlist" name="roomlist" onChange="selectRoom(this)">
					<option selected="selected">강의실선택</option>
				<c:forEach var="roomlist" items="${roomlist}">
					<option value="${roomlist.code}">${roomlist.code}(${roomlist.name})</option>
				</c:forEach>
				</select>
			</td>
			<td align="center"><input type="hidden" id="sigan" value=""><span id="sigan1">강의시간</span></td>
			<td align="center"><input type="hidden" id="curr_inwon" value="0"><span id="curr_inwon1">0</span></td>
			<td align="center"><input type="text" id="full_inwon" size="5"></td>
			<td align="center"><input type="hidden" id="full_yn" value="N"><span id="full_yn1">N</span></td>
			<td align="center"><input type="radio" name="close_yn" value="Y"/>예 <input type="radio" name="close_yn" value="N" checked="checked"/>아니오</td>
			<td align="center">
			<input type="button" value="저장" onclick="insertSugang()">
			</td>
		</tr>
</table>
</div>

<h2>삭제하기</h2>
<div>
	<input type="button" value="삭제" onclick="deleteSugang()">
</div>

<h2>수강신청 과목</h2>
<div id="sugangAdminTable2" class="defTable">
	<table id="output" >
		<tr id="thfix">
			<th></th>
			<th>과목코드<br>(과목명)</th>
			<th>교수코드<br>(교수명)</th>
			<th>학점</th>
			<th>전공구분</th>
			<th>강의실코드<br>(강의실)</th>
			<th>강의시간</th>
			<th>신청인원</th>
			<th>가능인원</th>
			<th>만원Y/N</th>
			<th>폐강여부</th>
			<th></th>
		</tr>
		
		<c:forEach var="article" items="${sugangadminlist}">
		<tr>
			<td>
				<input type="checkbox" name="admin_seq" value="${article.admin_seq}" />
			</td>
			<td>${article.gwamok_code}<br>(${article.gwamok_name})</td>
			<td>${article.gyosu_code}<br>(${article.gyosu_name})</td>
			<td>${article.hakjum}</td>
			<td>${article.junkong_gubun}</td>
			<td>${article.room_code}<br>(${article.room_name})</td>
			<td>${article.sigan}</td>
			<td>${article.curr_inwon}</td>
			<td>${article.full_inwon}</td>
			<td>${article.full_yn}</td>
			<td>${article.close_yn}</td>
			<td>
				<input type="button" value="수정" onclick="updateSugang('${article.admin_seq}','${article.gwamok_code}','${article.gwamok_name}','${article.gyosu_code}','${article.gyosu_name}','${article.hakjum}','${article.junkong_gubun}','${article.room_code}','${article.room_name}','${article.sigan}','${article.curr_inwon}','${article.full_inwon}','${article.full_yn}','${article.close_yn}')">
			</td>
		</tr>
		</c:forEach>
	</table>
</div>
</body>
</html>